#include "draw.h"
#include "nt35510.h"
#include "usart1.h"
static uint8_t count;
static uint8_t flag;
uint32_t temp_x = 0, temp_y = 0;
/**
 * 简介：画板界面及功能
 * 参数1:触摸屏的x轴
 * 参数2:触摸屏的y轴
 */
void Draw_app(int32_t x,int32_t y)
{
    choose_color(x, y);
}

//选择线的大小
void choose_Line_size(int32_t x,int32_t y, uint8_t num)
{
    
    if (x > 100 && x < 200 && y < 70)  //选中第一号字体
    {
        count = 1;
    }
    if (x > 100 && x < 200 && y >= 70 && y < 140)  // 选中第二号字体
    {
        count = 2;
    }
    if (x > 100 && x < 200 && y >= 140 && y < 210) //选中第三号字体
    {
        count = 3;
    }
    if (x > 100 && x < 200 && y >= 210 && y < 280) //选中第四号字体
    {
        count = 4;
    }
    if (x > 100 && x < 200 && y >= 280 && y < 350) //选中第五号字体
    {
        count = 5;
    }
    if (x > 100 && x < 200 && y >= 350 && y < 420) //选中第六号字体
    {
        count = 6;
    }

    if (flag == 1)
    {
        Draw_Point_count(x, y, BLACK);
    }
    if (flag == 2)
    {
        Draw_Point_count(x, y, BLUE3);
    }
    if (flag == 3)
    {
        Draw_Point_count(x, y, RED);
    }
    if (flag == 4)
    {
        Draw_Point_count(x, y, GREEN);
    }
    if (flag == 5)
    {
        Draw_Point_count(x, y, YELLOW);
    }
    if (flag == 6)
    {
        Draw_Point_count(x, y, BRED);
    }
    if (flag == 7)
    {
        Draw_Point_count(x, y, WHITE);
    }
    
}

//
void Draw_Point_count(int32_t x,int32_t y, uint16_t color)
{
    
    if (x > 854)
        return;
    if (count == 1 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 2, color);
    }
    if (count == 2 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 4, color);
    }
    if (count == 3 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 6, color);
    }
    if (count == 4 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 8, color);
    }
    if (count == 5 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 10, color);
    }
    if (count == 6 && x > 201)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 12, color);
    }

    if (count == 7)
    {
        NT35510_DrawLineCircle(temp_x, temp_y, x, y, 12, color);
    }

    temp_x = x;   //记录上一次坐标的值，在 GTP_Touch_Up 函数中清除
    temp_y = y;
}

/**
  * @brief  在两点之间描绘轨迹
  * @param  x1: 上一次x轴坐标
  * @param  y1: 上一次y轴坐标
  * @param  x2: 当前x轴坐标
  * @param  y2: 当前y轴坐标
  * @param  thick:像素点
  * @retval None
  */
#define ABS(X)  ((X) > 0 ? (X) : -(X))
void NT35510_DrawLineCircle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t thick, uint16_t color)
{
  int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, 
  yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, 
  curpixel = 0;
  
  deltax = ABS(x2 - x1);        /* The difference between the x's */
  deltay = ABS(y2 - y1);        /* The difference between the y's */
  x = x1;                       /* Start x off at the first pixel */
  y = y1;                       /* Start y off at the first pixel */
  
	
  if (x2 >= x1)                 /* The x-values are increasing */
  {
    xinc1 = 1;
    xinc2 = 1;
  }
  else                          /* The x-values are decreasing */
  {
    xinc1 = -1;
    xinc2 = -1;
  }
  
  if (y2 >= y1)                 /* The y-values are increasing */
  {
    yinc1 = 1;
    yinc2 = 1;
  }
  else                          /* The y-values are decreasing */
  {
    yinc1 = -1;
    yinc2 = -1;
  }
  
  if (deltax >= deltay)         /* There is at least one x-value for every y-value */
  {
    xinc1 = 0;                  /* Don't change the x when numerator >= denominator */
    yinc2 = 0;                  /* Don't change the y for every iteration */
    den = deltax;
    num = deltax / 2;
    numadd = deltay;
    numpixels = deltax;         /* There are more x-values than y-values */
  }
  else                          /* There is at least one y-value for every x-value */
  {
    xinc2 = 0;                  /* Don't change the x for every iteration */
    yinc1 = 0;                  /* Don't change the y when numerator >= denominator */
    den = deltay;
    num = deltay / 2;
    numadd = deltax;
    numpixels = deltay;         /* There are more y-values than x-values */
  }
  
  for (curpixel = 0; curpixel <= numpixels; curpixel++)
  {
		
		//判断边界
		if(x+thick>LCD_X_LENGTH || x-thick<0 || //液晶左右边界
			y+thick>LCD_Y_LENGTH || y-thick<0  ) //液晶上下边界
			continue;
    Draw_Point(x, y, thick<<1, color);
    //LCD_Color_Init(x, thick<<1, y, thick<<1, color);
//    NT35510_DrawCircle(x,y,thick,1);             /* Draw the current pixel */
    num += numadd;              /* Increase the numerator by the top of the fraction */
    if (num >= den)             /* Check if numerator >= denominator */
    {
      num -= den;               /* Calculate the new numerator value */
      x += xinc1;               /* Change the x as appropriate */
      y += yinc1;               /* Change the y as appropriate */
    }
    x += xinc2;                 /* Change the x as appropriate */
    y += yinc2;                 /* Change the y as appropriate */
  }
}



//选择颜色
void choose_color(int32_t x,int32_t y)
{
    if (x < 100 && y < 70)  //选中黑色
    {
        flag = 1;
    }
    if (x < 100 && y >= 70 && y < 140)  // 选中浅蓝色
    {
        flag = 2;
    }
    if (x < 100 && y >= 140 && y < 210) //选中红色
    {
        flag = 3;
    }
    if (x < 100 && y >= 210 && y < 280) //选中绿色
    {
        flag = 4;
    }
    if (x < 100 && y >= 280 && y < 350) //选中黄色
    {
        flag = 5;
    }
    if (x < 100 && y >= 350 && y < 420) //选中粉色
    {
        flag = 6;
    }
    if (x < 100 && y >= 420 && y < 480)  //清屏
    {
        LCD_Color_Init(0, LCD_X_LENGTH, 0, LCD_Y_LENGTH, WHITE);
        Draw_color(0, WHITE, BLACK, 0, 0);
    }
    if (x > 100 && x <200 && y >= 420 && y < 480)  //橡皮檫
    {
        flag = 7;
        count = 7;
    }

    switch (flag)
    {
        case 1:
            Draw_color(1, WHITE, BLACK, x, y);
            break;
        case 2:
            Draw_color(2, WHITE, BLUE3, x, y);
            break;
        case 3:
            Draw_color(3, WHITE, RED, x, y);
            break;
        case 4:
            Draw_color(4, WHITE, GREEN, x, y);
            break;
        case 5:
            Draw_color(5, WHITE, YELLOW, x, y);
            break;
        case 6:
            Draw_color(6, WHITE, BRED, x, y);
            break;
        case 7:
            Draw_color(7, WHITE, BRED, x, y);
            break;

        default:
            break;
    }
}

//左边颜色
/**
 * 参数1: 改变颜色的序号 1~6
 * 参数2:序号的颜色
 * 参数3: 线的颜色
 */
void Draw_color(uint8_t num, uint16_t back_color, uint16_t line_color, int32_t x,int32_t y)
{
    switch (num)  //序号
    {
        case 1:
            LCD_Color_Init(0, 100, 0, 70, back_color);       
            LCD_Color_Init(0, 100, 70, 140, BLUE3);      
            LCD_Color_Init(0, 100, 140, 210, RED);       
            LCD_Color_Init(0, 100, 210, 280, GREEN);     
            LCD_Color_Init(0, 100, 280, 350, YELLOW);    
            LCD_Color_Init(0, 100, 350, 420, BRED);      
            break;
        case 2:
            LCD_Color_Init(0, 100, 0, 70, BLACK);            
            LCD_Color_Init(0, 100, 70, 140, back_color); 
            LCD_Color_Init(0, 100, 140, 210, RED);       
            LCD_Color_Init(0, 100, 210, 280, GREEN);     
            LCD_Color_Init(0, 100, 280, 350, YELLOW);    
            LCD_Color_Init(0, 100, 350, 420, BRED);      
            break;
        case 3:
            LCD_Color_Init(0, 100, 0, 70, BLACK);             
            LCD_Color_Init(0, 100, 70, 140, BLUE3);       
            LCD_Color_Init(0, 100, 140, 210, back_color); 
            LCD_Color_Init(0, 100, 210, 280, GREEN);      
            LCD_Color_Init(0, 100, 280, 350, YELLOW);     
            LCD_Color_Init(0, 100, 350, 420, BRED);       
            break;
        case 4:
            LCD_Color_Init(0, 100, 0, 70, BLACK);             
            LCD_Color_Init(0, 100, 70, 140, BLUE3);       
            LCD_Color_Init(0, 100, 140, 210, RED);        
            LCD_Color_Init(0, 100, 210, 280, back_color); 
            LCD_Color_Init(0, 100, 280, 350, YELLOW);     
            LCD_Color_Init(0, 100, 350, 420, BRED);       
            break;
        case 5:
            LCD_Color_Init(0, 100, 0, 70, BLACK);              
            LCD_Color_Init(0, 100, 70, 140, BLUE3);        
            LCD_Color_Init(0, 100, 140, 210, RED);         
            LCD_Color_Init(0, 100, 210, 280, GREEN);       
            LCD_Color_Init(0, 100, 280, 350, back_color);  
            LCD_Color_Init(0, 100, 350, 420, BRED);        
            break;
        case 6:
            LCD_Color_Init(0, 100, 0, 70, BLACK);              
            LCD_Color_Init(0, 100, 70, 140, BLUE3);        
            LCD_Color_Init(0, 100, 140, 210, RED);         
            LCD_Color_Init(0, 100, 210, 280, GREEN);       
            LCD_Color_Init(0, 100, 280, 350, YELLOW);      
            LCD_Color_Init(0, 100, 350, 420, back_color);  
            break;
        
        default:
            LCD_Color_Init(0, 100, 0, 70, BLACK);      // 1    
            LCD_Color_Init(0, 100, 70, 140, BLUE3);    // 2
            LCD_Color_Init(0, 100, 140, 210, RED);     // 3
            LCD_Color_Init(0, 100, 210, 280, GREEN);   // 4
            LCD_Color_Init(0, 100, 280, 350, YELLOW);  // 5
            LCD_Color_Init(0, 100, 350, 420, BRED);    // 6
            break;
    }
    NT35510_DisString(5, 430, "clean");      
    NT35510_DisString(103, 430, "rubber");   

    Draw_line(100, 1, 0, 480, BLACK);       //边框 
    Draw_line(100, 100, 70, 1, BLACK);
    Draw_line(100, 100, 140, 1, BLACK);
    Draw_line(100, 100, 210, 1, BLACK);
    Draw_line(100, 100, 280, 1, BLACK);
    Draw_line(100, 100, 350, 1, BLACK);
    Draw_line(100, 100, 420, 1, BLACK);
    Draw_line(200, 1, 0, 480, BLACK);

    Draw_Line_Size(line_color);
    choose_Line_size(x, y, num);
}

//线的大小
void Draw_Line_Size(int16_t color)
{
    Draw_line(105, 90, 35, 2, color);
    Draw_line(105, 90, 102, 6, color);
    Draw_line(105, 90, 170, 12, color);
    Draw_line(105, 90, 238, 18, color);
    Draw_line(105, 90, 305, 24, color);
    Draw_line(105, 90, 372, 30, color);
}
